#include <map>
#include "BeginConnectOriDestHandler.h"
#include "EndConnectOriDestHandler.h"

using namespace std;
using namespace log4cplus;

Logger BeginConnectOriDestHandler::log=GetLogger(LOG4CPLUS_TEXT("BeginConnectOriDestHandler"));

void BeginConnectOriDestHandler::CreateSocketPostProcess(ClientContext &client,OVERLAPPED *ovl,SOCKET sock)
{
	map<string,string>::const_iterator iter=client.HttpRequest().Headers().find("Proxy-Connection");
	if(iter!=client.HttpRequest().Headers().end()&&iter->second==" Keep-Alive")
	{
		setsockopt(sock,SOL_SOCKET,SO_KEEPALIVE,reinterpret_cast<char*>(1),sizeof(BOOL));
	}
	client.SetDestSock(sock);
	LOG4CPLUS_DEBUG_STR(log,LOG4CPLUS_TEXT("Original server socket post process finished."));
}

ITransHandler* BeginConnectOriDestHandler::GetNextHandler()
{
	return new EndConnectOriDestHandler();
}

ITransHandler::ExecuteResultCode BeginConnectOriDestHandler::OnExecuteError(ClientContext &client,OVERLAPPED *ovl,ITransHandler::ExecuteResultCode executeResult)
{
	return executeResult;
}